import Taro, { Component } from '@tarojs/taro'
import { Swiper, SwiperItem, Image, View, Text } from '@tarojs/components'
import '../../assets/common.scss'
import CommonJs from '../../utils/common'
import '../activitybm/activitybm.scss'

export default class Index extends Component {

	constructor(props) {
    super(props);
    this.state = {
     	toucheck:'PROFITSHARING',
     	type:'',
     	typename:'',
     	setinfo:{},
     	assetsAmount:'',
     	auth:'',
     	bankid:''
    }
  }

  config = {
    navigationBarTitleText: '提现'
  }

  opendetails(url,id){
    this.$preload('activityId',id);
    CommonJs.openTo(url);
  }
 
 
  componentWillMount () {
 	var type=this.$router.params.type
 	var str=''
 	if(type=='BALANCE_RMB'){
 		str='金额'
 	}
 	else{
 		str='共享金'
 	}
 	this.setState({
      type:type,
      typename:str
    });
    this.getdata()
    this.isauth()
  }

  componentWillUnmount () {
    
  }
  componentDidShow () {
	var pages = getCurrentPages();
	var Page = pages[pages.length - 1];
	var info = Page.data;
	if(info.data){
		if(info.data.bankPaytype){
			this.setState({
		      bankid:info.data.id,
		    });
		}
	}
  }
  isauth(){
  	var url='/pay/papi/v1.9/authorize-record/user/Authorize'
  	var _this=this;
    CommonJs.ajaxRequest(url,'','GET',function(res){
        _this.setState({
          auth: res.data
        });
    },'wfw')
  }
  getdata(){
   
    var url='/pay/papi/v1.9/withdrawal-setting/find/accountId'
    var _this=this;
    CommonJs.ajaxRequest(url,'','GET',function(res){
        _this.setState({
          setinfo: res.data
        });
    },'wfw')
  }
  payment(val){
  	this.setState({
      toucheck:val
   });
    if(val=='TRANSFER'){
    	this.open('/pages/my/bank?type=TRANSFER')
    }
  }
  open(url){
  	CommonJs.openTo(url);
  }
  sure(){
  	var url='/papi/v1.9/withdrawal-record'
  	var assetsAmount =this.state.assetsAmount;
  	if(!assetsAmount){
  		Taro.showToast({
		  title:'请输入提现金额',
		  icon: 'none',
		  duration: 2000
		})
  		return false;
  	}
  	if(assetsAmount<200||assetsAmount%100!=0){
  		Taro.showToast({
		  title:'提现金额应大于200元，并且是100的整数倍',
		  icon: 'none',
		  duration: 2000
		})
  		return false;
  	}
  	if(this.state.toucheck=='PROFITSHARING'){
  		this.getopenid()
  	}
  	else{
  		this.getsjnum()
  	}
  }
  getopenid(){
  	var _this=this
  	var realName =this.state.realName;
  	if(!this.state.auth&&realName||this.state.auth){
	  	Taro.login({
	      success(res) {
	        if(res.code){
	         var code=res.code
	         var url='/pay/papi/v1.9/authorize-record/userCodeAuthorize'
	         var dates={
	         	code:code,
	         	realName:realName
	         }
	         CommonJs.ajaxRequest(url,dates,'POST',function(res){
			   	_this.getsjnum()
			 },'wfw')
	        }else{
	           Taro.showToast({
				  title:'网络异常',
				  icon: 'none',
				  duration: 2000
				})
	        }
	      }
	    })
	}
  	else{
  		Taro.showToast({
		  title:'微信提现需要校验用户姓名，请如实填写真实姓名',
		  icon: 'none',
		  duration: 2000
		})
  	}
  }
  getsjnum(){
  	var _this=this
  	var setinfo=this.state.setinfo
  	//var url='/pay/papi/v1.9/withdrawal-setting/withdrawal/'+this.state.type+'/'+this.state.assetsAmount*100
  	var url='/pay/papi/v1.9/withdrawal-setting/withdrawalAssetsType?amount='+this.state.assetsAmount*100+'&assetsType='+this.state.type+'&type='+this.state.toucheck
  	CommonJs.ajaxRequest(url,'','GET',function(res){
  		var str="实际到账金额："+res.data/100+"元"
		Taro.showModal({
		  title: '操作提示',
		  content:str,
		  showCancel:true,
		  success: function (res) {
		    if (res.confirm) {
		     _this.txfun()
		    } 
		  }
		})       
 	},'wfw')
  }
  txfun(){
  	var _this=this;
  	var url='/pay/papi/v1.9/withdrawal-record'
  	var dates={
  		assetsAmount:this.state.assetsAmount*100,
  		authorizeType:'APPLETS',
  		type:this.state.toucheck,
  		assetsType:this.state.type
  	}
  	if(this.state.toucheck=='TRANSFER'){
  		
  		dates.bankId=this.state.bankid
  	}
  	CommonJs.ajaxRequest(url,dates,'POST',function(res){
  		Taro.showToast({
		  title:'提现申请成功，平台会在1-3天内审核',
		  icon: 'none',
		  duration: 2000
		})
  		setTimeout(function(){
  			_this.open('/pages/my/txlist')
  		},1000)
  	},'wfw')
  }
  handleChange(name,e){
    var _this = this
      let value=e.target.value;
      this.setState({
      [name]: value
    });
  }
  render () {
    const setinfo=this.state.setinfo
   return (
    	<View className='page'>
	       	<View className='qun-bmgue'>
		       	<View className='qun-gueli qun-guelibrto'>
		       		<View className='qun-bmzlo'>
		       			<Text className='qun-bmsuwn'>可提现金额</Text>
		       		</View>
		       		<View className='qun-bmsuj qun-jagnrg qun-color1'>
		       			<View className='qun-jagn qun-color1' style={this.state.type=='BALANCE_RMB'?'float:right;':'display:none'}>
		       				{setinfo.balanceRmb/100}
		       			</View>
		       			<View className='qun-jagn qun-color1' style={this.state.type=='BALANCE_GXJ'?'float:right;':'display:none'}>
		       				{setinfo.balanceGxj/100}
		       			</View>
		       		</View>
		       	</View>
		       	<View className='qun-gueli qun-guelibrto'>
		       		<View className='qun-bmzlo'>
		       			<Text className='qun-bmsuwn'>提现类型</Text>
		       		</View>
		       		<View className='qun-bmsuj qun-jagnrg qun-color1'>
		       			<View className='qun-jagn qun-color1' style="float:right;">
		       				{this.state.typename}
		       			</View>
		       		</View>
		       	</View>
		       	<View className='qun-gueli qun-guelibrto'>
		       		<View className='qun-bmzlo'>
		       			<Text className='qun-bmsuwn'>提现比例</Text>
		       		</View>
		       		<View className='qun-bmsuj qun-jagnrg qun-color1'>
		       			<View className='qun-jagn qun-color1' style={this.state.type=='BALANCE_RMB'?'float:right;':'display:none'}>
		       				{setinfo.balanceRmbRatio}%
		       			</View>
		       			<View className='qun-jagn qun-color1' style={this.state.type=='BALANCE_GXJ'?'float:right;':'display:none'}>
		       				{setinfo.balanceGxjRatio}%
		       			</View>
		       		</View>
		       	</View>
		       	<View className='qun-gueli qun-guelibrto'>
		       		<View className='qun-bmzlo'>
		       			<Text className='qun-bmsuwn'>税费</Text>
		       		</View>
		       		<View className='qun-bmsuj qun-jagnrg qun-color1'>
		       			<View className='qun-jagn qun-color1' style={this.state.type=='BALANCE_RMB'?'':'display:none'}>
		       				{setinfo.rmbBrokerage}‰
		       			</View>
		       			<View className='qun-jagn qun-color1' style={this.state.type=='BALANCE_GXJ'?'':'display:none'}>
		       				{setinfo.gxjBrokerage}‰
		       			</View>
		       		</View>
		       	</View>
		    </View>
		   
	       <View className='qun-bmgue'>
	       	<View className='qun-gueli'>
	       		<View className='qun-bmzlo'>
	       			<Text className='qun-bmsuwn'>提现金额</Text>
	       		</View>
	       		<View className='qun-bmsuj'>
	       			<View className='qun-jagn'>
	       				<Input className='' onInput={this.handleChange.bind(this,'assetsAmount')} maxLength='10'  type='text' placeholder='请输入'/>
	       			</View>
	       		</View>
	       	</View>
	       	
	       	<View className='qun-gueli .qun-guelibrto' style={this.state.auth?'display:none':''}>
	       		<View className='qun-bmzlo'>
	       			<Text className='qun-bmsuwn'>真实姓名</Text>
	       		</View>
	       		<View className='qun-bmsuj'>
	       			<View className='qun-jagn'>
	       				<Input className='' onInput={this.handleChange.bind(this,'realName')} placeholder='请如实填写'  type='text' />
	       			</View>
	       		</View>
	       	</View>
	       </View>
	       <View className='qun-bmgue'>
		       	<View className='qun-bmtet' style="border-bottom:1px solid #eee;font-size:30rpx">提现到(第三方手续费{setinfo.brokerage}‰)</View>
		       	<View classNme='qun-zifans'>
		       		<View className='qun-zifuli' onClick={this.payment.bind(this,'PROFITSHARING')}>
		       			<View className='qun-fasi'>
		       				
		       				<View className='qun-wexm'>
		       					<Text>微信</Text>
		       				</View>
		       			</View>
		       			<Image className='qun-bmbgtu' src={this.state.toucheck=='PROFITSHARING' ? 'https://minichat.g2ma.com/kedouimg/icon24.png' : 'https://minichat.g2ma.com/kedouimg/icon23.png'}></Image>
		       		</View>
		       		<View className='qun-zifuli' onClick={this.payment.bind(this,'TRANSFER')}>
		       			<View className='qun-fasi'>
		       				
		       				<View className='qun-wexm'>
		       					<Text>银行卡</Text>
		       				</View>
		       			</View>
		       			<Image className='qun-bmbgtu' src={this.state.toucheck=='TRANSFER' ? 'https://minichat.g2ma.com/kedouimg/icon24.png' : 'https://minichat.g2ma.com/kedouimg/icon23.png'}></Image>
		       		</View>
		       	</View>
	       </View>
	       <View className="withdraw-note" style="padding:30rpx 3% 150rpx 3%;color:#666">
				<View className="tips">
					<Text>1-3个工作日内完成,提现金额最小200，必须是100的整数倍</Text>
				</View>
				<View className="note">
					<View>银行处理可能有延迟，具体以账户的到账时间为准。</View>
					<View>税费从剩余的资金账户扣除，剩余资金不足于支付税费则不予提现。</View>
				</View>
			</View>
	       <View className='qun-bmljzf'  onClick={this.sure.bind(this)}>确定提现</View>
		
      </View>
    )
  }
}
